﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.Web;
using Module;

namespace HospitalManager.QuanLyDuoc.HeThongBaoCao.BaoCaoTongHopPhieuNhapKho
{
    public partial class BaoCaoTongHopPhieuNhapKho : Page
    {
        private readonly Module.QuanLyDuoc.HeThongBaoCao _heThongBaoCao = new Module.QuanLyDuoc.HeThongBaoCao();
        private readonly SelectAllClass _selectAllClass = new SelectAllClass();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (Session["report"] != null)
                    rptBaoCao.Report = Session["report"] as XtraReport;
            }
            else
            {
                LoadDMKho();
                LoadDMNhaCungCap();
                txtTuNgay.Text = DateTime.Now.ToString("dd-MM-yyyy");
                txtDenNgay.Text = DateTime.Now.ToString("dd-MM-yyyy");
                XemBaoCao();
            }
        }

        protected void rptBaoCao_Unload(object sender, EventArgs e)
        {
            ((ReportViewer)sender).Report = null;
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            XemBaoCao();
        }

        private void LoadDMKho()
        {
            ddlKho_Khoa.DataSource = _selectAllClass.SelectAllKho();
            ddlKho_Khoa.DataTextField = "TenKho";
            ddlKho_Khoa.DataValueField = "MaKho";
            ddlKho_Khoa.DataBind();
            ddlKho_Khoa.Items.Insert(0, new ListItem("Chọn kho nhập", ""));
        }

        private void LoadDMNhaCungCap()
        {
            ddlNhaCungCap.DataSource = _selectAllClass.SelectAllNhaCungCap();
            ddlNhaCungCap.DataTextField = "TenNhaCungCap";
            ddlNhaCungCap.DataValueField = "ID";
            ddlNhaCungCap.DataBind();
            ddlNhaCungCap.Items.Insert(0, new ListItem("Chọn nhà cung cấp", ""));
        }

        private void XemBaoCao()
        {
            // Fill Thông tin bệnh viện tới datatable ThongTinBenhVien trong dataset dsHeThongBaoCao
            var _dsHeThongBaoCao = new dsHeThongBaoCao();
            var thongtinbenhvien = _selectAllClass.ThongTinBenhVien();
            foreach (DataRow row in thongtinbenhvien.Rows)
            {
                _dsHeThongBaoCao.Tables["ThongTinBenhVien"].ImportRow(row);
            }
            // Fill dữ liệu báo cáo
            var thongtinbaocao =
                _heThongBaoCao.BaoCaoTongHopPhieuNhapKho(
                    Convert.ToDateTime(DateTime.ParseExact(txtTuNgay.Text, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                    Convert.ToDateTime(DateTime.ParseExact(txtDenNgay.Text, "dd-MM-yyyy", null).ToString("yyyy-MM-dd")),
                    ddlKho_Khoa.SelectedValue.Trim(), ddlNhaCungCap.SelectedValue.Trim());
            foreach (DataRow row in thongtinbaocao.Rows)
            {
                _dsHeThongBaoCao.Tables["BaoCaoTongHopPhieuNhapKho"].ImportRow(row);
            }
            var report = new rptBaoCaoTongHopPhieuNhapKho
            {
                DataSource = _dsHeThongBaoCao
            };
            rptBaoCao.Report = report;
            report.FindControl("txtTuNgay", true).Text = DateTime.ParseExact(txtTuNgay.Text.Trim(), "dd-MM-yyyy", null).ToString("dd-MM-yyyy");
            report.FindControl("txtDenNgay", true).Text = DateTime.ParseExact(txtDenNgay.Text.Trim(), "dd-MM-yyyy", null).ToString("dd-MM-yyyy");
            report.FindControl("txtTenKho", true).Text = ddlKho_Khoa.SelectedValue == "" ? "" : ddlKho_Khoa.SelectedItem.Text;
            report.FindControl("txtNhaCungCap", true).Text = ddlNhaCungCap.SelectedValue == "" ? "" : ddlNhaCungCap.SelectedItem.Text;
            Session["report"] = report;
        }
    }
}